#include "udpServer.hpp"
#include <memory>
using namespace Server;
#include <unordered_map>
#include <fstream>
#include <signal.h>
#include <stdio.h>


void my_Errno(char *n)
{
    printf("请正确输入#%s,port\n", n);
}



//demo1:字典翻译：客户端输入单词，服务端翻译后返回给客户端
void handlerMessage(int sockfd, string clientip, uint16_t clientport, string message)
{
    string respose_message = message;
    respose_message+="[server echo]";

    // 开始返回
    struct sockaddr_in client;
    client.sin_family = AF_INET;
    client.sin_port = htons(clientport);
    client.sin_addr.s_addr = inet_addr(clientip.c_str());

    // 将客户端发送的数据里面的value值返回
    sendto(sockfd, respose_message.c_str(), sizeof(respose_message), 0, (sockaddr *)&client, sizeof(client));
}




int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        my_Errno(argv[0]);
        exit(1);
    }

    uint16_t port = atoi(argv[1]);

    unique_ptr<udpServer> ret(new udpServer(handlerMessage, port));

    ret->initServer();
    ret->start();

    return 0;
}
